// ex06.c -- generate 1000 random numbers in [1, 10]
// count the number of each number

#include <stdio.h>
#include <stdlib.h>

static int seed = 37;

// copied from ex05.c
int randomInt(const int lb, const int ub);
int *countNumbers(int lb, int ub, int len, int arr[len]);

int main(int argc, char *argv[]) {
  int arr[1000];

  for (int k = 0; k < 10; ++k) {
    for (int i = 0; i < 1000; ++i) {
      arr[i] = 0;
    }

    printf("seed: ");
    scanf("%d", &seed);

    for (int i = 0; i < 1000; ++i) {
      arr[i] = randomInt(1, 10);
    }

    for (int i = 0; i < 10; ++i) {
      printf("%d\t", i + 1);
    }
    puts("");

    int *res = countNumbers(1, 10, 1000, arr);
    for (int i = 0; i < 10; ++i) {
      printf("%d\t", res[i]);
    }
    free(res);
    putchar('\n');
  }

  return 0;
}

int randomInt(const int lb, const int ub) {
  seed = (seed * 373 + 57632) % 32637;

  return (seed + lb) % ub + 1;
}

int *countNumbers(int lb, int ub, int len, int arr[len]) {
  int *count = calloc(ub - lb + 1, sizeof(int));
  for (int i = 0; i < len; ++i) {
    ++count[arr[i] - 1];
  }

  return count;
}
